<?php

class EntityCache {

    /*memcache intance*/
    private static $cache = false;
    /**内部缓存*/
    private static $cacheData = array();
    private static $inited = false;
    private static $serverList = array("localhost","192.168.1.144");

    private function __construct() {
    }

    public static function init($serverList) {
        self::$serverList = $serverList;
        if (self::$serverList === NULL || !is_array(self::$serverList)){
        	self::$serverList = array();
        }
    }

    private static function doInit() {
        if(self::$inited) {
            return;
        }
        self::$inited = true;
        $serverCount = 0;
        $serverList = self::$serverList;
        $connected = false;
        foreach ($serverList as $server) {
            if (preg_match("{^(.+?)(:(\d+))?$}",$server,$result)) {
                self::$cache = new Memcache();
                $host = $result[1];
                $port = 11211;
                if (isset($result[3])) {
                    $port = $result[3];
                }
                $connected = @self::$cache->pconnect($host,$port);
                if ($connected) {
                    break;
                }
            }
        }
        if ($connected==false) {
            self::$cache = false;
        }
    }

    /**
     * 取得memcache实例.
     *
     * @return Memcache
     */
    public static function getCache() {
        self::doInit();
        return self::$cache;
    }

    public static function getStats() {
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        return $cache->getStats();
    }

    public static function get($key) {
        if (isset(self::$cacheData[$key])) {
            return self::$cacheData[$key];
        }
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        $result = $cache->get($key);
        if ($result !== false) {
        	self::$cacheData[$key] = $result;
        }
        return $result;
    }
    
	public static function batchGet($keys) {
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        return $cache->get($keys);
    }

    public static function set($key,$var, $flag=0, $expire=0) {
        self::$cacheData[$key] = $var;
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        $cache->set($key,$var,0,$expire);
    }

    /**
     * 不保存到内部缓存,方便长时间运行的程序使用缓存.
     */
    public static function directSet($key, &$var, $flag=0, $expire=0) {
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        $cache->set($key,$var,$flag,$expire);
    }

    public static function delete($key,$timeout=0) {
        unset(self::$cacheData[$key]);
        $cache = self::getCache();
        if ($cache===false) {
            return false;
        }
        return $cache->delete($key,$timeout);
    }

    static function clear(){
    	self::$cacheData = array();
    }
}

class CacheLock {
    private function __construct() {
    }

    public static function lock($lockTag,$time=3) {
        $cacheKey = "lock_".$lockTag;
        for($i=0;$i<$time;$i++) {
            if (EntityCache::get($cacheKey)===false) {
                break;
            }
            sleep(1);
        }
        EntityCache::set($cacheKey,1,0,$time);
    }

    public static function unlock($lockTag) {
        $cacheKey = "lock_".$lockTag;
        EntityCache::delete($cacheKey);
    }

}
?>